#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/** Autuor : zhaoshun
  * Time   : 2017/12/03 21:05
  * Email  : tmby1314@163.com
  * student ID: 716101010107
  * */

int main_not_name_pipe(int argc, char const *argv[]) {

    if (NULL == argv[1] || NULL == argv[2]) {
        printf("Usage : copy file1 file1");
        exit(10);
    }

    char const *src_file = argv[1];
    char const *dest_file = argv[2];

    const int size = 1014;
    int sfd, dfd, n, chan[2];
    char buff[size];
    sfd = open(src_file, O_RDONLY, 0644);
    dfd = open(dest_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    pipe(chan);
    if (fork()) {
        while((n=read(sfd,buff,size))>0) {
            close(chan[0]);
            write(chan[1], buff, n);
            close(chan[1]);
        }
    } else {
        read(chan[0], buff, size);
        write(dfd, buff, strlen(buff));
        close(chan[0]);
    }
    close(sfd);
    close(dfd);
    return 0;
}